<#ftl output_format="HTML" >
<#import "/layout.ftl" as layout>
<#import "/page.ftl" as pageTemplate>
<@layout.masterTemplate navIndex=3 >
<div class="card mt-4 mb-4" >
    <form id="queryForm" class="card-body">
        <div class="form-inline mb-2">
            <div class="form-group">
                <label for="category" class="sr-only" >栏目：</label>
                <@spring.formSingleSelectWithAll "form.categoryId" allCategories 'class="form-control mr-2"' />
            </div>
            <div class="form-group">
                <label for="title" class="sr-only">标题：</label>
                <@spring.formInput "form.title" 'class="form-control mr-2" style="width:20em" maxlength="20" placeholder="标题" autofocus' 'search' />
            </div>
            <div class="form-group">
                <label for="keyword" class="sr-only">关键字：</label>
                <@spring.formInput "form.keyword" 'class="form-control mr-2" style="width:20em" maxlength="40" placeholder="关键字"' 'search' />
            </div>
        </div>
        <div class="form-inline mb-2">
            <div class="form-group">
                <label for="updater" class="sr-only">修改人：</label>
                <@spring.formInput "form.updater" 'class="form-control mr-2" placeholder="修改人"' />
            </div>
            <div class="form-group">
                <label for="startDate" class="sr-only">修改日期：</label>
                <@spring.formInputDate "form.startDate" 'class="form-control" placeholder="起始日期"' />
                <span class="input-group-text" > - </span>
                <@spring.formInputDate "form.endDate" 'class="form-control mr-2" placeholder="终止日期"' />
            </div>
            <div class="form-group">
                <button type="submit" id="query" class="btn btn-primary">查询</button>
            </div>
        </div>
        <div class="form-inline">
            <div class="form-group">
                <button type="submit" id="query" class="btn btn-primary mr-2">查询</button>
                <button type="button" id="create" class="btn btn-info mr-2">创建</button>
                <button type="button" id="publish" class="btn btn-info mr-2">发布</button>
                <button type="button" id="revoke" class="btn btn-info mr-2">撤回</button>
                <button type="button" id="delete" class="btn btn-warning mr-2">删除</button>
            </div>
        </div>
    </form>
</div>
<div class="table-responsive" >
<table class="as-table table table-hover table-striped table-condensed">
<tr>
    <th class="text-right">ID</th>
    <th class="text-center">类型</th>
    <th class="text-center">标题</th>
    <th class="text-center">修改人</th>
    <th class="text-center">修改时间</th>
    <th class="text-center">状态</th>
</tr>
<#list presentations as presentation>
<tr id="r${presentation.id}">
    <td class="text-right"><label for="cb${presentation.id}" ><input type="checkbox" name="presentationSel" value="${presentation.id}" id="cb${presentation.id}" data-id="${presentation.id}" />&nbsp;${presentation.id}</label></td>
    <td class="text-center">
<#if presentation.category??>
    ${presentation.category.title!""}
</#if>
    </td>
    <td class="text-center"><a href="<@spring.url '/presentation/${presentation.id}'/>" data-tooltip="${presentation.title?js_string}" style="max-width:24em" class="as-literal" >${presentation.title}</a></td>
    <td class="text-center">${presentation.updater}</td>
    <td class="text-center">${presentation.lastModifiedTime?datetime}</td>
    <td class="text-center">
<#if presentation.status == 0>
        <span class="badge badge-secondary">草稿</span>
<#elseif presentation.status == 1>
        <span class="badge badge-info">待审核</span>
<#elseif presentation.status == 2>
        <span class="badge badge-success">审核通过</span>
<#elseif presentation.status == 3>
        <span class="badge badge-light">已撤回</span>
<#else>
        <span class="badge badge-warning">(${presentation.status})</span>
</#if>
    </td>
</tr>
</#list>
</table>
</div>
<@pageTemplate.page startRowIndex=page.startRowIndex maximumRows=20 totalCount=presentationCount />
<script type="text/javascript">
'use strict';
let baseUrl = '<@spring.url "/presentation" />';
$('#queryForm').submit(evt => {
    let newUrl = baseUrl + '/index(' + combineArgs(['categoryId', 'title', 'keyword', 'updater', 'startDate', 'endDate']) + ')';
    location.href = newUrl + '?_t=' + new Date().getTime();
    evt.preventDefault();
});
$('#create').click(evt => location.href = baseUrl + '/create');
$('#publish').click(evt => {
    let selection = getSelectionByName('presentationSel');
    let btn = this;
    if (selection.length > 0) {
        let idArray = selection.join(',');
        if (confirm('确认发布介绍【' + idArray + '】？之后还可以撤回')) {
            btn.disabled = true;

            let url = baseUrl + '/publication/' + selection.join(',');
            rpcPut({url: url})
            .then(data => {
                reloadPage(true);
            }).catch((xhr, status, err) => {
                console.error(xhr.responseText);
            }).then(function() {
                btn.disabled = false;
            });
        }
    }
});
$('#revoke').click(evt => {
    let selection = getSelectionByName('presentationSel');
    let btn = this;
    if (selection.length > 0) {
        let idArray = selection.join(',');
        if (confirm('确认撤回介绍【' + idArray + '】？之后还可以重新发布')) {
            btn.disabled = true;

            let url = baseUrl + '/revoke/' + selection.join(',');
            rpcDelete({url: url})
            .then(data => {
                reloadPage(true);
            }).catch((xhr, status, err) => {
                console.error(xhr.responseText);
            }).then(function() {
                btn.disabled = false;
            });
        }
    }
});
$('#delete').click(evt => {
    let selection = getSelectionByName('presentationSel');
    let btn = this;
    if (selection.length > 0) {
        let idArray = selection.join(',');
        if (confirm('确认删除介绍【' + idArray + '】？此操作无法撤销！')) {
            btn.disabled = true;

            let url = baseUrl + '/' + selection.join(',');
            rpcDelete({url: url})
            .then(() => reloadPage(true))
            .finally(() => btn.disabled = false);
        }
    }
});
</script>
</@layout.masterTemplate>
